package com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.entity.SmsPaymentExamineEquipmentProcurementProcess;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.entity.req.SmsPaymentExamineEquipmentProcurementProcessReq;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.entity.resp.SmsPaymentExamineEquipmentProcurementProcessResp;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.mapper.SmsPaymentExamineEquipmentProcurementProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.service.SmsPaymentExamineEquipmentProcurementProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.constant.examineObjectTypeConstant.EXAMINE_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.constant.examineObjectTypeConstant.EXAMINE_OBJECT_TYPE_ROLE;

/**
 * <p>
 * 付款审批流程（设备采购） 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-04
 */
@Service
public class SmsPaymentExamineEquipmentProcurementProcessServiceImpl extends ServiceImpl<SmsPaymentExamineEquipmentProcurementProcessMapper, SmsPaymentExamineEquipmentProcurementProcess> implements SmsPaymentExamineEquipmentProcurementProcessService {
    @Autowired
    private SmsPaymentExamineEquipmentProcurementProcessMapper smsPaymentExamineEquipmentProcurementProcessMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private SmsRoleMapper smsRoleMapper;

    @Override
    public ResponseResult updatePaymentExamineProcess(SmsPaymentExamineEquipmentProcurementProcessReq processReq) {
        if (isNullOrEmpty(processReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if (isNullOrEmpty(processReq.getProcessObjectType())) {//如果流程对象类型不是空
//            if (isNullOrEmpty(processReq.getProcessObjectId())) {//流程对象id如果为空
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
//            }
        }

        SmsPaymentExamineEquipmentProcurementProcess process =
                smsPaymentExamineEquipmentProcurementProcessMapper.selectById(processReq.getId());
        if (process == null || isNullOrEmpty(process.getId())) {
            return CommonResult.failed(CommonCodeEnum.PAYMENT_PROCESS_NOT_EXIST);
        }

        if (processReq.getProcessObjectType() != null && !isNullOrEmpty(processReq.getProcessObjectId())) {
            if (processReq.getProcessObjectType() == EXAMINE_OBJECT_TYPE_PERSON && !processReq.getProcessIsApplicantEdit()) {
                SmsUser smsUser = cacheUtil.getUserInfo(processReq.getProcessObjectId());
                if (smsUser == null || isNullOrEmpty(smsUser.getId())) {
                    return CommonResult.failed(CommonCodeEnum.PAYMENT_PROCESS_USER_NOT_EXIST);
                }
            } else if (processReq.getProcessObjectType() == EXAMINE_OBJECT_TYPE_ROLE) {
                SmsRole smsRole = smsRoleMapper.selectById(processReq.getProcessObjectId());
                if (smsRole == null || isNullOrEmpty(smsRole.getId())) {
                    return CommonResult.failed(CommonCodeEnum.PAYMENT_PROCESS_ROLE_NOT_EXIST);
                }
            }
        }

        SmsPaymentExamineEquipmentProcurementProcess updateProcess = new SmsPaymentExamineEquipmentProcurementProcess();
        updateProcess.setId(processReq.getId());
        if (!isNullOrEmpty(processReq.getProcessObjectType())) {
            updateProcess.setProcessObjectId(processReq.getProcessObjectId());
            updateProcess.setProcessObjectType(processReq.getProcessObjectType());
        }
        if (!isNullOrEmpty(processReq.getProcessIsApplicantEdit())) {
            updateProcess.setProcessIsApplicantEdit(processReq.getProcessIsApplicantEdit());
        }
        if (!isNullOrEmpty(processReq.getProcessIsSign())) {
            updateProcess.setProcessIsSign(processReq.getProcessIsSign());
        }
        if (!isNullOrEmpty(processReq.getProcessIsStamp())) {
            updateProcess.setProcessIsStamp(processReq.getProcessIsStamp());
        }
        if (!isNullOrEmpty(processReq.getProcessIsUse())) {
            updateProcess.setProcessIsUse(processReq.getProcessIsUse());
        }

//        if (!isNullOrEmpty(processReq.getProcessName())){
//            updateProcess.setProcessName(processReq.getProcessName());
//        }
        smsPaymentExamineEquipmentProcurementProcessMapper.updateById(updateProcess);
        return CommonResult.success();
    }

    //查询审批流程
    @Override
    public ResponseResult getPaymentExamineProcessList() {
        List<SmsPaymentExamineEquipmentProcurementProcessResp> respList =
                smsPaymentExamineEquipmentProcurementProcessMapper.selectList(new QueryWrapper<SmsPaymentExamineEquipmentProcurementProcess>()
                        .lambda()
                        .orderByAsc(SmsPaymentExamineEquipmentProcurementProcess::getProcessSort))
                        .stream()
                        .map(process -> {
                            SmsPaymentExamineEquipmentProcurementProcessResp processResp =
                                    new SmsPaymentExamineEquipmentProcurementProcessResp();
                            BeanUtils.copyProperties(process, processResp);

                            if (process.getProcessObjectType() != null && !isNullOrEmpty(process.getProcessObjectId())) {
                                if (process.getProcessObjectType() == EXAMINE_OBJECT_TYPE_PERSON) {
                                    SmsUser smsUser = cacheUtil.getUserInfo(process.getProcessObjectId());
                                    if (smsUser != null && !isNullOrEmpty(smsUser.getId())) {
                                        SmsSearchUserResp userResp = new SmsSearchUserResp();
                                        BeanUtils.copyProperties(smsUser, userResp);
                                        processResp.setProcessObjectName(smsUser.getRealName());
                                        processResp.setUserInfo(userResp);
                                    }
                                } else {
                                    SmsRole smsRole = smsRoleMapper.selectById(process.getProcessObjectId());
                                    if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                                        processResp.setProcessObjectName(smsRole.getRoleName());
                                    }
                                }
                            }
                            return processResp;
                        })
                        .collect(Collectors.toList());
        return CommonResult.success(respList);
    }
}
